package mail

import (
	"AiBus/global"
	"AiBus/request"
	"github.com/jordan-wright/email"
	"net/smtp"
	"strconv"
)

func EmailInit(Tomail string) *email.Email {
	newEmail := email.NewEmail()
	//	发送方邮箱
	newEmail.From = "lgb <654109102@qq.com>"
	//	设置接收方的邮箱
	newEmail.To = []string{Tomail}
	//newEmail.To = []string{"admiwj@outlook.com"}
	//	设置主题
	newEmail.Subject = "待办通知"

	return newEmail

}

// 授权码：zwgfivoildjibaie
func SendMail(Tomail string, req request.ToDoReq) error {
	class_id := strconv.Itoa(*req.ToDoClass)
	PriorityID := strconv.Itoa(*req.Priority)
	listContent := req.Title + "：\n" + "待办类别：" + class_id + "\n" + "待办优先级：" + PriorityID + "\n" + req.Desc + "\n" + "待办开始时间：" + req.StartTime + "\n待办结束时间：" + req.OverTime + "\n"

	mail_client := EmailInit(Tomail)
	mail_client.Text = []byte(listContent)
	err := mail_client.Send("smtp.qq.com:25", smtp.PlainAuth("", global.App.Configuration.Email.Mail, global.App.Configuration.Email.Code, "smtp.qq.com"))
	if err != nil {
		global.App.Log.Error(err.Error())
		return err
	}
	return nil
}
